MyDesktop.Definitions = Ext.extend(Ext.app.Module, {
	
    init : function(){
        this.launcher = {
            text: _('Definitions'),
            iconCls:'definitions',
            handler : this.createWindow,
            windowId : 'DefMan',
            scope: this
        }
    },

    createWindow : function(src){
        var desktop = this.app.getDesktop();
        var winname = src.windowId;
        var win = desktop.getWindow(winname);

        if(!win){
			var cm = new Ext.grid.ColumnModel(
				[{
			       header: _('Name'),
			       dataIndex: 'name',
			       width: 220,
			       editor: new Ext.form.TextField({
			           allowBlank: false,
/*
			           enableKeyEvents: true,
			            listeners: {
			                'keypress': {
			                    fn: function(t){
			                        var v = t.getValue(),
			                            wc = 0, cc = v.length ? v.length : 0;
			                            
			                        if(cc > 0){
			                            wc = v.match(/\b/g);
			                            wc = wc ? wc.length / 2 : 0;
			                        }
				                    Ext.fly(wordCount.getEl()).update('Words: '+wc);
			                        Ext.fly(charCount.getEl()).update('Chars: '+cc);
				                },
			                    buffer: 1 // buffer to allow the value to update first
			                }
			            }
			           */
			       })
			    },{
			       header: _('Value'),
			       dataIndex: 'value',
			       width: 130,
			       editor: new Ext.form.TextField({
			           allowBlank: true
			       })
			    }]);

			cm.defaultSortable = true;

		    var DefRecord = Ext.data.Record.create([
		           {name: 'name', type: 'string', mapping: 'id'},
		           {name: 'value', type: 'string'}
		      ]);

			var store = new Ext.data.Store({
				proxy: new Ext.data.HttpProxy({
					url: getMainUrl(),
					method: 'POST'
					}),
					baseParams: {action: 'defman'},
				reader: new Ext.data.JsonReader({
						root: 'results',
						id: 'name',
					},DefRecord),
					sortInfo: {field: 'name',direction:'ASC'}
			});

		    var grid = new Ext.grid.EditorGridPanel({
		        store: store,
		        cm: cm,
		        clicksToEdit:1,
		        selModel: new Ext.grid.RowSelectionModel(),
		        tbar: [
			        {
			            text: _('Add'),
			            iconCls: 'add',
			            handler : function(){
			                var p = new DefRecord({
			                	name: '',
			                	value: ''
			                });
			                grid.stopEditing();
			                store.insert(0, p);
			                grid.startEditing(0, 0);
			            }
			        },
			        {
			            text: _('Save'),
			            iconCls: 'save',
			            handler : function(){
			                store.commitChanges();
			            }
			        },
		        ]
		    });

			grid.on({
				'beforeedit': {
					fn: function(e) {
						if (e.field=="name" && !e.record.isModified("name") && e.record.data.name!="") return false;
					}
				},
				'rowcontextmenu': {
					fn: function onDefManContextMenu(grid, rowIndex, e) {
					    e.stopEvent();
					    grid.selModel.selectRow(rowIndex);
					
					    var listContextMenu = new Ext.menu.Menu({
					    	items: [
					    		{
					    			iconCls: 'remove',
					    			text: _('Delete'),
					    			handler: function() {
										Verbum.Util.confirm(_('Sure?'), function() {
													var util = new Verbum.Utilities();
													util.request({
											            params: { 
											               action: "defman", 
											               name: grid.selModel.getSelected().data.name,
											               op: 'rm'
											              }											
													});	
													util.on({
														'done': function() {
															store.reload();
														}
													});
										});	
					    			}
					    		}
					    	]
					    });
					    
					    listContextMenu.showAt(e.getXY());
					}
				}			
			});
						
			store.on({
				'update': {
					fn: function(store,record,op) {
						if (Ext.data.Record.COMMIT==op) {
							Verbum.Util.request({
					            params: { 
					               action: "defman", 
					               name: record.data.name,
					               value: record.data.value,
					               op: 'save'
					              }											
							});
						}
					}
				}	
			});
			
            win = desktop.createWindow({
				id: winname,
				title: src.text,
				closable:true,
				width:450,
				height:350,
				plain:true,
				layout: 'fit',
				iconCls: 'definitions',
				items: grid
            });

            store.load();
        }
        win.show();
    },
	display : function(){
        this.createWindow();
	}
});
